package com.alone.mybatisplus.demo.entity;

import cn.hutool.core.collection.CollUtil;
import com.alone.mybatisplus.datascope.annotation.MainScope;
import com.alone.mybatisplus.datascope.annotation.Scope;
import com.alone.mybatisplus.demo.ExtendMapper;
import com.alone.mybatisplus.datascope.conts.DataScopeModel;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.Collections;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author Alone
 * @date 2022/6/20 4:37 下午
 */
@Scope(DataScopeModel.DEPT)
@Mapper
public interface DeptMapper extends ExtendMapper<Dept> {

    @Select("select id from dept where pid = #{pid}")
    Set<Long> findByPid(Long pid);

    default Set<Long> recursiveFindByPid(Long pid) {
        Set<Long> res = findByPid(pid);
        if (CollUtil.isNotEmpty(res)) {
            for (Long deptId : res) {
                res.addAll(recursiveFindByPid(deptId));
            }
        }
        return res;
    }

}